define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    // del_url: 'user/user/del',
                    set_teacher_url: 'user/user/set_teacher',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        //{field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'is_vip', title: __('是否Vip'), searchList: {"0":__('No'),"1":__('Yes')}, formatter: Table.api.formatter.normal},
                        {field: 'vip_expire_time', title: __('Vip到期时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'is_teacher', title: __('Is_teacher'), searchList: {"0":__('No'),"1":__('Yes')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'set_teacher',
                                text: __('Set teacher'),
                                title: __('Set teacher'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-user',
                                url: 'user/user/set_teacher'
                            }, {
                                name: 'consume_log',
                                text: __('消费记录'),
                                title: __('消费记录'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-list',
                                url: 'user/user/consume_log',
                                extend: 'data-area=["80%","80%"]',
                                visible: function(row) {
                                    return true;  // 所有用户都显示
                                }
                            }]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        set_teacher: function () {
            Controller.api.bindevent();
            
        },
        consume_log: function () {
            // 从URL中获取ids参数
            var ids = Fast.api.query('ids');
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/consume_log/ids/' + ids,
                    table: 'user_consume_log',
                }
            });

            var table = $("#table");
            
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                sortOrder: 'desc',
                search: false,
                commonSearch: true,
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), visible: false},
                        {field: 'type', title: __('Type'), searchList: {
                            "vip": __('会员'),
                            "goods": __('商品'),
                            "travel": __('文旅'),
                            "service": __('服务'),
                            "withdraw": __('提现'),
                            "refund": __('退款'),
                        }, formatter: Table.api.formatter.normal, operate: 'LIKE'},
                        {field: 'target_title', title: __('消费项目'), operate: 'LIKE'},
                        {field: 'amount', title: __('金额'), operate: false, formatter: function(value) {
                            return value > 0 ? '<span class="text-success">+' + value + '</span>' : '<span class="text-danger">' + value + '</span>';
                        }},
                        {field: 'createtime', title: __('消费时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime}
                    ]
                ],
                onLoadSuccess: function (data) {
                    if (data && data.extend) {
                        $('.panel-lead span.text-success,.panel-lead span.text-danger').html(data.extend.total_amount);
                        $('.panel-lead span').removeClass('text-success text-danger').addClass(data.extend.total_amount >= 0 ? 'text-success' : 'text-danger');
                    }
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});